package com.zito.health.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zito.health.common.annotations.ResponseResult;
import com.zito.health.common.constant.BaseConstant;
import com.zito.health.common.result.PageResult;
import com.zito.health.common.result.VerifyResult;
import com.zito.health.core.util.DigestsUtil;
import com.zito.health.entity.Unit;
import com.zito.health.entity.User;
import com.zito.health.service.UnitService;
import com.zito.health.service.UserService;
import com.zito.health.util.CacheUtil;
import com.zito.health.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @author: gxt
 * @time: 2019/10/12 10:29
 */
@Slf4j
@ResponseResult
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UnitService unitService;

    @PostMapping()
    public VerifyResult save(User user){
        VerifyResult verifyResult = new VerifyResult();
        if(user.getRoleId()==2||user.getRoleId()==3||
                user.getRoleId()==8||user.getRoleId()==9||user.getRoleId()==10){
            Integer flag = unitService.verificationSingle(user.getUnitId(),user.getRoleId());
            if(flag!=0){
                verifyResult.setResult(false);
                verifyResult.setMsg("该角色每个单位只能有一个账号！");
                return verifyResult;
            }
        }else if(user.getRoleId()==5){//科研人员必须有区域
            Unit unit=new Unit();
            unit =unitService.queryById(user.getUnitId());
            user.setAreaId(unit.getAreaId());
        }

        if(user.getEnabled() ==2){//注册要用2
        }else{//管理员直接添加的
            user.setEnabled(1);
        }
        user.setPassword(DigestUtils.md5Hex(user.getPassword().trim()));
        userService.save(user);
        return verifyResult;
    }

    @GetMapping
    public PageResult list(UserVo param, @RequestParam(defaultValue = "0") Integer pageNo, @RequestParam(defaultValue = "10")Integer pageSize){
        PageResult<UserVo> result = new PageResult();
//        param.setEnabled(1);
        if(pageNo!=null&&pageSize!=null){
            PageHelper.startPage(pageNo, pageSize);
        }
        List<UserVo> list = userService.queryList(param);
        PageInfo<UserVo> pageInfo = new PageInfo<UserVo>(list);
        result.setTotal(pageInfo.getTotal());
        result.setRows(pageInfo.getList());
        return result;
    }

    @PutMapping
    public VerifyResult update(User user){
        VerifyResult verifyResult = new VerifyResult();
        User u= new User();
        u.setUsername(user.getUsername());
        u.setUserid(user.getUserid());
        List<User> list= userService.validateUsername4Update(u);
        if(list==null){
            verifyResult.setMsg("该用户已被删除！");
            verifyResult.setResult(false);
            return verifyResult;
        }else if(list.size()>0){
            verifyResult.setMsg("该用户名已被其他用户占用！");
            verifyResult.setResult(false);
            return verifyResult;
        }
        user.setPassword(DigestUtils.md5Hex(user.getPassword().trim()));
        userService.update(user);
        return verifyResult;
    }

    /**
     * 审批
     * @param user
     * @return
     */
    @PutMapping("/approval")
    public VerifyResult approval(User user){
        VerifyResult verifyResult = new VerifyResult();
        userService.update(user);
        return verifyResult;
    }

    @DeleteMapping("/{id}")
    public VerifyResult delete(@PathVariable Integer id){
        VerifyResult verifyResult = new VerifyResult();
        userService.delete(id);
        return verifyResult;
    }

    @GetMapping("/{id}")
    public User queryById(@PathVariable Integer id){
        User user = userService.queryById(id);
        return user;
    }

    @GetMapping("/validateUsername")
    public VerifyResult validateUsername(String username){
        VerifyResult verifyResult = new VerifyResult();
        User param = new User();
        param.setUsername(username);
        User user=userService.queryOne(param);
        if(user==null){
            verifyResult.setResult(Boolean.FALSE);
        }
        return verifyResult;
    }

    @GetMapping("/getUserInfo")
    public User getUserInfo(){
        User cacheUser = CacheUtil.getCacheUser().getUser();
        User user = userService.queryUserById(cacheUser.getUserid());
        return user;
    }

    /**
     * admin账号列表
     * @param param
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/list4Admin")
    public PageResult list4Admin(UserVo param, @RequestParam(defaultValue = "0") Integer pageNo,
                                 @RequestParam(defaultValue = "10")Integer pageSize){
        PageResult<UserVo> result = new PageResult();
//        param.setEnabled(1);
        if(pageNo!=null&&pageSize!=null){
            PageHelper.startPage(pageNo, pageSize);
        }
        List<UserVo> list = userService.list4Admin(param);
        PageInfo<UserVo> pageInfo = new PageInfo<UserVo>(list);
        result.setTotal(pageInfo.getTotal());
        result.setRows(pageInfo.getList());
        return result;
    }







}
